using System;
using System.IO;
using System.Collections;

namespace FridayShopping.DataModel
{

	public class StatisticsInfo : ItemBase, IComparable
	{
		private int count = 0;
		private int totalAmount = 0;
		
		#region Members Access
		public int Count 
		{
			get
			{
				return count;
			}
			set 
			{
				count = value;
			}
		}

		public int TotalAmount
		{
			get
			{
				return totalAmount;
			}
			set
			{
				totalAmount = value;
			}
		}
		
		#endregion

		#region IComparable Members

		public new int CompareTo(object obj)
		{
			return (((StatisticsInfo)obj).Count - this.Count);
		}

		#endregion
	}

	/// <summary>
	/// Manage the data colleted from shoppings to perform various stat opeations
	/// </summary>
	public class Statistics : IItemList
	{		
		static private Statistics instance;

		private Hashtable	statInfo;

		private Statistics()
		{
		}

		static public Statistics Instance 
		{
			get
			{
				if (instance == null) 
				{
					instance = new Statistics();
				}
				return instance;
			}
		}
        
		public void ExportListToCSV(Categories categories) 
		{
			string fileName = Database.Instance.ExportCSVFile;

			bool append = (File.Exists(fileName));

			StreamWriter sw = new StreamWriter(fileName, true /*append*/, 
				System.Text.Encoding.Unicode);
			
			if (append == false) 
			{
				sw.WriteLine("Cat Key\tCat Name\tProd Key\tProd Name\tAmount");
			}

			DateTime d = DateTime.Now;					
			sw.WriteLine(d.ToShortDateString() + "\t" + d.ToLongTimeString());

			foreach (Category category in categories.List)
			{
				foreach(Product product in category.Products.List) 
				{
					// Only purchased items are counted
					if (product.Purchased > 0 )
					{
						string line = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t",
							product.CategoryKey.ToString(),category.Name,
							product.Key.ToString(),product.Name,product.Purchased);

						sw.WriteLine(line);
					}
				}
			}
			sw.Close();		
		}

		public void ResetStatistics()
		{
			string fileName = Database.Instance.ExportCSVFile;
			
			if (File.Exists(fileName))
			{
				File.Delete(fileName);
			}
		}

		public void Load(string fileName)
		{
			ArrayList data = new ArrayList();

			if (!File.Exists(fileName)) 
			{
				throw new StatisticsException("Statistics file not found");
			}
			StreamReader reader = new StreamReader(fileName);
			string line;
			while((line = reader.ReadLine()) != null) 
			{
				// It's not the header
				if (char.IsDigit(line[0])) 
				{
					// and it's not the date
					if ((line.Split('\t')).Length >= 5) 
					{
						Product product = new Product(line);
						data.Add(product);
					}
				}
			}	
			reader.Close();

			// Collate info into a hash table
			statInfo = new Hashtable();
			foreach (Product product in data)
			{
				StatisticsInfo info = (StatisticsInfo)statInfo[product.Key];
				if (info == null) 
				{
					info = new StatisticsInfo();
				} 
				info.Key = product.Key;
				info.Count++;
				info.TotalAmount+=product.Amount;
				statInfo[product.Key] = info;
			}
		}

		#region Implement the IItemList interface
		public ArrayList List
		{
			get 
			{
				ArrayList list = new ArrayList();
				foreach(StatisticsInfo info in statInfo.Values)
				{
					list.Add(info);
				}
				list.Sort();
				return list;
			}
			set
			{
			}
		}

		public ItemBase GetItem(Key key)
		{
			return null;
		}

		public void MoveUp(ItemBase item)
		{
		}

		public void MoveDown(ItemBase item)
		{
		}
		public void Delete(Key key)
		{
		}
		public Key Add(ItemBase item)
		{
			return new Key(0);
		}
		#endregion
	}

	public class StatisticsException : DatabaseException
	{
		public StatisticsException(string message) : base(message)
		{
		}
	}
}
